﻿#region DaEmployee
/*----------------------------------------------------------------
// 文件名：DaEmployee.cs
// 功能描述：表dbo.Employee的数据访问层
//
// 
// 创建时间：2009-11-20 DtataAccess template . Ver 5.0.20090413
//
// 修改标识： 
// 修改描述： 
//----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Globalization;
using System.Text;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

using EQT.Model;


namespace EQT.Dal
{
    ///可以继承更上层接口,IEmployee ,数据访问层基类DaBase，
    ///DaBase中包含了DbException异常处理静态方法
    public partial class DaEmployee : DaBase<MoEmployee>
    {
        #region SQL const
        internal const string TABLE_COLUMNS = " EMP_ID ,DEPT_ID ,FULLNAME ,SEX_CODE ,IDCARD ,BIRTHDAY ,PHONE ,HOME_ADDR ,REM   ";

        internal const string SQL_INSERT = "INSERT INTO EMPLOYEE (" + TABLE_COLUMNS + ") VALUES (@EmpId,@DeptId,@Fullname,@SexCode,@Idcard,@Birthday,@Phone,@HomeAddr,@Rem)";
        internal const string SQL_SELECT = "SELECT " + TABLE_COLUMNS + " FROM EMPLOYEE  ";
        internal const string SQL_SELECT_ONE = SQL_SELECT + " WHERE EMP_ID=@EmpId";

        internal const string SQL_EXIST = "SELECT COUNT(*) FROM EMPLOYEE  WHERE EMP_ID=@EmpId  ";
        internal const string SQL_UPDATE = "UPDATE EMPLOYEE SET DEPT_ID=@DeptId, FULLNAME=@Fullname, SEX_CODE=@SexCode, IDCARD=@Idcard, BIRTHDAY=@Birthday, PHONE=@Phone, HOME_ADDR=@HomeAddr, REM=@Rem WHERE EMP_ID=@EmpId";

        internal const string SQL_DELETE_DEFAULT = "DELETE FROM EMPLOYEE ";
        internal const string SQL_DELETE = "DELETE FROM EMPLOYEE WHERE EMP_ID=@EmpId";

        internal const string SQL_COUNT = "SELECT COUNT(*) FROM Employee ";
        #endregion

        #region Constructor

        /// <summary>
        /// 默认构造函数，使用配置文件中默认的数据库配置。
        /// </summary>
        public DaEmployee()
        {
            this.db = DatabaseFactory.CreateDatabase();
        }
        /// <summary>
        /// 使用指定的databaseName数据库配置。
        /// </summary>
        /// <param name="databaseName">配置文件中数据库的配置名称。</param>
        public DaEmployee(string databaseName)
        {
            this.db = DatabaseFactory.CreateDatabase(databaseName);
        }

        public DaEmployee(Database db)
        {
            this.db = db;
        }

        #endregion

        //以下是CRUD方法，每个方法都有引入外部事务的版本

        #region 构造器模块

        internal override MoEmployee ConstructT()
        {
            return new MoEmployee();
        }
        #endregion



        #region Add模块 Helper

        ///build the command object.It never throw exception.
        internal override DbCommand ConstructAddCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_INSERT);

            db.AddInParameter(dbCommand, "EmpId", DbType.AnsiString);
            db.AddInParameter(dbCommand, "DeptId", DbType.AnsiString);
            db.AddInParameter(dbCommand, "Fullname", DbType.String);
            db.AddInParameter(dbCommand, "SexCode", DbType.AnsiStringFixedLength);
            db.AddInParameter(dbCommand, "Idcard", DbType.AnsiString);
            db.AddInParameter(dbCommand, "Birthday", DbType.DateTime);
            db.AddInParameter(dbCommand, "Phone", DbType.AnsiString);
            db.AddInParameter(dbCommand, "HomeAddr", DbType.String);
            db.AddInParameter(dbCommand, "Rem", DbType.String);
            return dbCommand;
        }
        internal override DbCommand ConstructAddCommand(Database db, MoEmployee entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_INSERT);
            db.AddInParameter(dbCommand, "EmpId", DbType.AnsiString, entity.EmpId);
            db.AddInParameter(dbCommand, "DeptId", DbType.AnsiString, entity.DeptId);
            db.AddInParameter(dbCommand, "Fullname", DbType.String, entity.Fullname);
            db.AddInParameter(dbCommand, "SexCode", DbType.AnsiStringFixedLength, entity.SexCode);
            db.AddInParameter(dbCommand, "Idcard", DbType.AnsiString, entity.Idcard);
            db.AddInParameter(dbCommand, "Birthday", DbType.DateTime, entity.Birthday);
            db.AddInParameter(dbCommand, "Phone", DbType.AnsiString, entity.Phone);
            db.AddInParameter(dbCommand, "HomeAddr", DbType.String, entity.HomeAddr);
            db.AddInParameter(dbCommand, "Rem", DbType.String, entity.Rem);
            return dbCommand;
        }

        internal override DbCommand PopulateAddCommandParameters(Database db, DbCommand addCmd, MoEmployee entity)
        {
            db.SetParameterValue(addCmd, "EmpId", entity.EmpId);
            db.SetParameterValue(addCmd, "DeptId", entity.DeptId);
            db.SetParameterValue(addCmd, "Fullname", entity.Fullname);
            db.SetParameterValue(addCmd, "SexCode", entity.SexCode);
            db.SetParameterValue(addCmd, "Idcard", entity.Idcard);
            db.SetParameterValue(addCmd, "Birthday", entity.Birthday);
            db.SetParameterValue(addCmd, "Phone", entity.Phone);
            db.SetParameterValue(addCmd, "HomeAddr", entity.HomeAddr);
            db.SetParameterValue(addCmd, "Rem", entity.Rem);
            return addCmd;
        }

        internal override DbCommand PopulateAddCommandParameters(Database db, DbCommand addCmd, DataRow row)
        {
            db.SetParameterValue(addCmd, "EmpId", row["EmpId"]);
            db.SetParameterValue(addCmd, "DeptId", row["DeptId"]);
            db.SetParameterValue(addCmd, "Fullname", row["Fullname"]);
            db.SetParameterValue(addCmd, "SexCode", row["SexCode"]);
            db.SetParameterValue(addCmd, "Idcard", row["Idcard"]);
            db.SetParameterValue(addCmd, "Birthday", row["Birthday"]);
            db.SetParameterValue(addCmd, "Phone", row["Phone"]);
            db.SetParameterValue(addCmd, "HomeAddr", row["HomeAddr"]);
            db.SetParameterValue(addCmd, "Rem", row["Rem"]);
            return addCmd;
        }

        #endregion

        #region Update模块 Helper

        internal override DbCommand ConstructUpdateCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_UPDATE);
            db.AddInParameter(dbCommand, "DeptId", DbType.AnsiString);
            db.AddInParameter(dbCommand, "Fullname", DbType.String);
            db.AddInParameter(dbCommand, "SexCode", DbType.AnsiStringFixedLength);
            db.AddInParameter(dbCommand, "Idcard", DbType.AnsiString);
            db.AddInParameter(dbCommand, "Birthday", DbType.DateTime);
            db.AddInParameter(dbCommand, "Phone", DbType.AnsiString);
            db.AddInParameter(dbCommand, "HomeAddr", DbType.String);
            db.AddInParameter(dbCommand, "Rem", DbType.String);
            db.AddInParameter(dbCommand, "EmpId", DbType.AnsiString);

            return dbCommand;
        }
        internal override DbCommand ConstructUpdateCommand(Database db, MoEmployee entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_UPDATE);
            db.AddInParameter(dbCommand, "DeptId", DbType.AnsiString, entity.DeptId);
            db.AddInParameter(dbCommand, "Fullname", DbType.String, entity.Fullname);
            db.AddInParameter(dbCommand, "SexCode", DbType.AnsiStringFixedLength, entity.SexCode);
            db.AddInParameter(dbCommand, "Idcard", DbType.AnsiString, entity.Idcard);
            db.AddInParameter(dbCommand, "Birthday", DbType.DateTime, entity.Birthday);
            db.AddInParameter(dbCommand, "Phone", DbType.AnsiString, entity.Phone);
            db.AddInParameter(dbCommand, "HomeAddr", DbType.String, entity.HomeAddr);
            db.AddInParameter(dbCommand, "Rem", DbType.String, entity.Rem);
            db.AddInParameter(dbCommand, "EmpId", DbType.AnsiString, entity.EmpId);

            return dbCommand;
        }

        internal override DbCommand PopulateUpdateCommandParameters(Database db, DbCommand updateCmd, MoEmployee entity)
        {
            db.SetParameterValue(updateCmd, "DeptId", entity.DeptId);
            db.SetParameterValue(updateCmd, "Fullname", entity.Fullname);
            db.SetParameterValue(updateCmd, "SexCode", entity.SexCode);
            db.SetParameterValue(updateCmd, "Idcard", entity.Idcard);
            db.SetParameterValue(updateCmd, "Birthday", entity.Birthday);
            db.SetParameterValue(updateCmd, "Phone", entity.Phone);
            db.SetParameterValue(updateCmd, "HomeAddr", entity.HomeAddr);
            db.SetParameterValue(updateCmd, "Rem", entity.Rem);
            db.SetParameterValue(updateCmd, "EmpId", entity.EmpId);
            return updateCmd;
        }
        internal override DbCommand PopulateUpdateCommandParameters(Database db, DbCommand updateCmd, DataRow row)
        {
            db.SetParameterValue(updateCmd, "DeptId", row["DeptId"]);
            db.SetParameterValue(updateCmd, "Fullname", row["Fullname"]);
            db.SetParameterValue(updateCmd, "SexCode", row["SexCode"]);
            db.SetParameterValue(updateCmd, "Idcard", row["Idcard"]);
            db.SetParameterValue(updateCmd, "Birthday", row["Birthday"]);
            db.SetParameterValue(updateCmd, "Phone", row["Phone"]);
            db.SetParameterValue(updateCmd, "HomeAddr", row["HomeAddr"]);
            db.SetParameterValue(updateCmd, "Rem", row["Rem"]);
            db.SetParameterValue(updateCmd, "EmpId", row["EmpId"]);
            return updateCmd;
        }

        internal override void PrepareDataAdapterCommand(Database db, out DbCommand dbInsertCommand,
                             out DbCommand dbUpdateCommand, out DbCommand dbDeleteCommand)
        {
            dbInsertCommand = db.GetSqlStringCommand(SQL_INSERT);
            #region set insert cmd parameters
            db.AddInParameter(dbInsertCommand, "EmpId", DbType.AnsiString, "EMP_ID", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "DeptId", DbType.AnsiString, "DEPT_ID", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "Fullname", DbType.String, "FULLNAME", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "SexCode", DbType.AnsiStringFixedLength, "SEX_CODE", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "Idcard", DbType.AnsiString, "IDCARD", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "Birthday", DbType.DateTime, "BIRTHDAY", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "Phone", DbType.AnsiString, "PHONE", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "HomeAddr", DbType.String, "HOME_ADDR", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "Rem", DbType.String, "REM", DataRowVersion.Current);
            #endregion
            dbUpdateCommand = db.GetSqlStringCommand(SQL_UPDATE);
            #region Set update cmd value parameters
            db.AddInParameter(dbUpdateCommand, "DeptId", DbType.AnsiString, "DEPT_ID", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "Fullname", DbType.String, "FULLNAME", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "SexCode", DbType.AnsiStringFixedLength, "SEX_CODE", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "Idcard", DbType.AnsiString, "IDCARD", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "Birthday", DbType.DateTime, "BIRTHDAY", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "Phone", DbType.AnsiString, "PHONE", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "HomeAddr", DbType.String, "HOME_ADDR", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "Rem", DbType.String, "REM", DataRowVersion.Current);
            #endregion
            #region set update cmd pk where parameters
            db.AddInParameter(dbUpdateCommand, "EmpId", DbType.AnsiString, "EMP_ID", DataRowVersion.Current);
            #endregion
            dbDeleteCommand = db.GetSqlStringCommand(SQL_DELETE);
            #region set delete cmd pk where parameters
            db.AddInParameter(dbDeleteCommand, "EmpId", DbType.AnsiString, "EMP_ID", DataRowVersion.Current);
            #endregion
        }
        #endregion

        #region Delete模块 Helper

        internal override DbCommand ConstructDeleteCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_DELETE);

            db.AddInParameter(dbCommand, "EmpId", DbType.AnsiString);

            return dbCommand;
        }
        internal override DbCommand ConstructDeleteCommand(Database db, MoEmployee entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_DELETE);

            db.AddInParameter(dbCommand, "EmpId", DbType.AnsiString, entity.EmpId);

            return dbCommand;
        }

        internal override DbCommand ConstructDeleteCommandForWhere(Database db, string where)
        {
            return db.GetSqlStringCommand(SQL_DELETE_DEFAULT + where);
        }

        internal override DbCommand PopulateDeleteCommandParameters(Database db, DbCommand deleteCmd, MoEmployee entity)
        {
            db.SetParameterValue(deleteCmd, "EmpId", entity.EmpId);
            return deleteCmd;
        }

        internal override DbCommand PopulateDeleteCommandParameters(Database db, DbCommand deleteCmd, DataRow row)
        {
            db.SetParameterValue(deleteCmd, "EmpId", row["EmpId"]);
            return deleteCmd;
        }

        #endregion helper

        #region Query模块 Helper

        internal override DbCommand ConstructQueryCommand(string condition)
        {
            return db.GetSqlStringCommand(SQL_SELECT + condition);
        }

        #endregion

        #region GetEntity(s)模块 Helper

        internal override DbCommand ConstructSelectOneCommand(Database db, MoEmployee entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_SELECT_ONE);

            db.AddInParameter(dbCommand, "EmpId", DbType.AnsiString, entity.EmpId);
            return dbCommand;
        }

        internal override void PopulateEntityByDataReader(IDataReader reader, ref MoEmployee entity)
        {
            if (!reader.IsDBNull(0)) entity.EmpId = reader.GetString(0);
            if (!reader.IsDBNull(1)) entity.DeptId = reader.GetString(1);
            if (!reader.IsDBNull(2)) entity.Fullname = reader.GetString(2);
            if (!reader.IsDBNull(3)) entity.SexCode = reader.GetString(3);
            if (!reader.IsDBNull(4)) entity.Idcard = reader.GetString(4);
            if (!reader.IsDBNull(5)) entity.Birthday = reader.GetDateTime(5);
            if (!reader.IsDBNull(6)) entity.Phone = reader.GetString(6);
            if (!reader.IsDBNull(7)) entity.HomeAddr = reader.GetString(7);
            if (!reader.IsDBNull(8)) entity.Rem = reader.GetString(8);
        }



        internal override DbCommand ConstructSelectConditionCommand(Database db, string condition)
        {
            return db.GetSqlStringCommand(SQL_SELECT + condition);
        }

        #endregion

        #region Count模块 Helper

        internal override DbCommand ConstructCountCommand(Database db)
        {
            return db.GetSqlStringCommand(SQL_COUNT);
        }

        internal override DbCommand ConstructCountConditionCommand(Database db, string condition)
        {
            return db.GetSqlStringCommand(SQL_COUNT + condition);
        }

        #endregion

        #region IsExist模块 Helper

        internal override DbCommand ConstructIsExistCommand(Database db, MoEmployee entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_EXIST);
            db.AddInParameter(dbCommand, "EmpId", DbType.AnsiString, entity.EmpId);
            return dbCommand;
        }

        #endregion



    }
}
#endregion

